<?php
/*
	结算管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?:'list';
$tabs=array(
	array('op'=>'list','title'=>'结算列表'),
	
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('balance',$parameter,$total);
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'结算管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'balancesn','title'=>'结算单号','type'=>'string'),
			array('name'=>'nickname','title'=>'代理商','type'=>'string'),
			array('name'=>'fee','title'=>'金额','type'=>'price'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'pay_type','title'=>'付款方式','type'=>'radio','options'=>'pay_types'),
			array('name'=>'pay_id','title'=>'付款单号','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>array(0,1))),
			array('type'=>'modal','op'=>'pay','title'=>'打款','show'=>array('status'=>1)),
		)),
		'search'=>array(
			array('type'=>'choose','name'=>'mid','title'=>'所属代理商','action'=>'member.get_member','plugin'=>'core'),
			array('type'=>'radio','name'=>'status','title'=>'佣金状态','options'=>'statuss'),
		),
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	$return['pay_types']=array(
		'1'=>'微信支付',
		'2'=>'支付宝支付',
		'3'=>'银联转账',
	);
	$return['statuss']=array(
		'0'=>'待审核',
		'1'=>'待打款',
		'2'=>'已打款',
		'4'=>'审核不通过',
	);
	exi($return);
}

if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['id']){
			exi('请先选择要审核的结算单！','error');
		}
		if(!$post['status']){
			exi('请先设置是否审核！','error');
		}
		$update=array(
			'status'=>intval($post['status']),
		);
		$this->update('balance',$update,array('id'=>intval($post['id'])));
		exi('审核成功！');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要审核的结算单！','error');
	}
	$item=$this->get('balance',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['fee']=$item['fee']/100;
	$item['nickname']=pdo_getcolumn('core_memebrs',array('id'=>$item['mid']),'nickname');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'balancesn','title'=>'结算单号'),
			array('type'=>'string','name'=>'fee','title'=>'金额'),
			array('type'=>'string','name'=>'nickname','title'=>'所属代理商'),
			array('type'=>'radio','name'=>'status','title'=>'是否审核','options'=>'statuss'),
		),
	);
	$return['statuss']=array(
		'1'=>'审核通过',
		'4'=>'审核不通过',
	);
	exi($return);
}
if($_RQ['op']=='pay'){
	//打款
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['id']){
			exi('请先选择要打款的结算单！','error');
		}
		if(!$post['pay_type']){
			exi('请先设置打款方式！','error');
		}
		$balance=$this->get('balance',array('id'=>intval($post['id'])));
		if(!$balance){
			exi('指定结算单不存在！','error');
		}
		$type=intval($post['pay_type']);
		if($type==1){
			$params=array(
				'mch_billno'=>$balance['balancesn'],
				'openid'=>$balance['mid'],
				'money'=>$balance['fee']/100,
				'remark'=>'代理商佣金打款',
			);
			cfc('wechat_pay')->mch_pay($params);
		}else{
			exi('打款方式不存在！','error');
		}
		$update=array(
			'status'=>2,
		);
		$this->update('balance',$update,array('id'=>intval($post['id'])));
		exi('打款成功！');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要打款的结算单！','error');
	}
	$item=$this->get('balance',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id'));
	
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			
			array('type'=>'radio','name'=>'pay_type','title'=>'打款方式','options'=>'pay_types'),
		),
	);
	$return['pay_types']=array(
		'1'=>'微信支付',
		
	);
	
	exi($return);
}
